﻿Imports System.Data
Imports System.Data.SqlClient
Partial Class ndaily
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        If Not Page.IsPostBack Then
            BindGrid()
        End If
    End Sub
    Public Sub Datagrid_OnCancel(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Public Sub Datagrid_OnEdit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        DataGrid1.EditItemIndex = e.Item.ItemIndex
        BindGrid()
    End Sub
    Public Sub Datagrid_OnUpdate(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
        Dim sSql As String
        Dim blnAddNew As Boolean = False
        Dim txtDiachiV As TextBox
        Dim txtThuonghieu As TextBox
        Dim txtKhuvucV As TextBox
        Dim txtghichuV As TextBox
        Dim txtCuahang As TextBox
        Dim txtDienthoai As TextBox
        Dim txtTT As TextBox
        Dim txtMocua As TextBox
        Dim txtBando As TextBox
        Dim txtHinhanh As TextBox
        Dim txtShowroom As TextBox
        Dim txtTenquan As TextBox

        '----------------Lay du lieu -------------
        txtDiachiV = e.Item.Cells(2).Controls(0)
        txtThuonghieu = e.Item.Cells(3).Controls(0)
        txtKhuvucV = e.Item.Cells(4).Controls(0)
        txtDienthoai = e.Item.Cells(5).Controls(0)
        txtCuahang = e.Item.Cells(6).Controls(0)
        txtShowroom = e.Item.Cells(7).Controls(0)
        txtTT = e.Item.Cells(8).Controls(0)
        txtMocua = e.Item.Cells(9).Controls(0)
        txtBando = e.Item.Cells(10).Controls(0)
        txtghichuV = e.Item.Cells(11).Controls(0)
        txtHinhanh = e.Item.Cells(12).Controls(0)
        txtTenquan = e.Item.Cells(13).Controls(0)
        '----------------Update du lieu -------------
        If e.Item.Cells(1).Text < 1 Then
            blnAddNew = True
        End If
        If blnAddNew = True Then
            ' Insert into
            sSql = " insert into daily (diachiV,tendailyV,dienthoai,khuvucV,cuahang,TT,mocua,idtinhthanh,bando,ghichuV,hinhanh,Showroom,tenquan)" & _
            "values(@diachiV,@thuongHieu,@dienthoai,@khuvucV,@cuahang,@TT,@mocua,@idtinhthanh,@bando,@ghichuV,@hinhanh,@Showroom,@tenquan) "
        Else
            'Update
            sSql = " UPDATE daily SET" & _
            " diachiV =@diachiV,tendailyv =@thuongHieu, khuvucV=@khuvucV " & _
            ", dienthoai =@dienthoai" & _
            ", cuahang =@cuahang " & _
            ", bando =@bando " & _
            ", TT =@TT, mocua=@mocua" & _
            ", ghichuV=@ghichuV" & _
            ", hinhanh=@hinhanh" & _
            ", Showroom=@Showroom" & _
            ", tenquan=@tenquan" & _
            " WHERE iddaily = @iddaily"
        End If
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idtinhthanh", Request.QueryString("c")))
        Else
            MyCommand.Parameters.Add(New SqlParameter("@iddaily", e.Item.Cells(1).Text))
        End If
        If txtTT.Text = "" Then txtTT.Text = "0"
        If txtCuahang.Text = "" Then txtCuahang.Text = "0"
        If txtShowroom.Text = "" Then txtShowroom.Text = "0"

        MyCommand.Parameters.Add(New SqlParameter("@diachiV", txtDiachiV.Text))
        MyCommand.Parameters.Add(New SqlParameter("@thuongHieu", txtThuonghieu.Text))
        MyCommand.Parameters.Add(New SqlParameter("@khuvucV", txtKhuvucV.Text))
        MyCommand.Parameters.Add(New SqlParameter("@ghichuV", txtghichuV.Text))
        MyCommand.Parameters.Add(New SqlParameter("@dienthoai", txtDienthoai.Text))
        MyCommand.Parameters.Add(New SqlParameter("@mocua", txtMocua.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", CBool(txtTT.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@cuahang", CBool(txtCuahang.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@bando", txtBando.Text))
        MyCommand.Parameters.Add(New SqlParameter("@hinhanh", txtHinhanh.Text))
        MyCommand.Parameters.Add(New SqlParameter("@Showroom", CBool(txtShowroom.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@tenquan", txtTenquan.Text))

        MyCommand.ExecuteNonQuery()
        myConn.Close()
        DataGrid1.EditItemIndex = -1
        BindGrid()
    End Sub
    Private Sub BindGrid()
        Dim idTinhThanh As Int16 = -1
        If IsNumeric(Request.QueryString("c")) Then
            idTinhThanh = Request.QueryString("c")
        End If

        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationManager.AppSettings("cons"))
        Dim sSql As String = "select iddaily,diachiV,tendailyV,khuvucV,dienthoai,ghichuV,cuahang,bando,hinhanh,mocua,TT,Showroom, tenquan  from daily where idtinhthanh=" & idTinhThanh & " order by idtinhthanh,khuvuce,cuahang desc, diachiV"
        If Request.QueryString("k") <> "" Then
            sSql = "select iddaily,diachiV,tendailyV,khuvucV,dienthoai,ghichuV,cuahang,bando,hinhanh,mocua,TT,Showroom, tenquan  from daily where idtinhthanh=" & idTinhThanh & " and diachiv like N'%" & Request.QueryString("k") & "%' " & " order by idtinhthanh,khuvuce,cuahang desc, diachiV"
        End If
        myConn.Open()
        Dim myAdapter As New SqlDataAdapter(sSql, myConn)
        Dim myDataSet As New DataSet
        myAdapter.Fill(myDataSet, sSql)
        dr = myDataSet.Tables(0).NewRow()
        dr(0) = -1
        dr(1) = "Thêm mới"
        myDataSet.Tables(0).Rows.InsertAt(dr, 0)
        DataGrid1.DataSource = myDataSet
        DataGrid1.DataBind()
        myConn.Close()
    End Sub
    Public Sub DataGrid1_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid1.PageIndexChanged
        DataGrid1.CurrentPageIndex = e.NewPageIndex
        BindGrid()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        BindGrid()
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Response.Redirect("ndaily.aspx?c=" & Request.QueryString("c") & "&k=" & TextBox1.Text)
    End Sub
End Class
